package com.lb.shopguide.adapter;

import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.goods.GoodsDetailBean;
import com.lb.shopguide.entity.goods.SkuBean;
import com.lb.shopguide.ui.view.LbGoodsNumSelector;
import com.lb.shopguide.util.pic.ImageLoaderUtils;
import com.yqritc.recyclerviewflexibledivider.FlexibleDividerDecoration;

import java.util.List;

/**
 * Created by kang on 2017/6/1.
 * 收银台
 */

public class AdapterGoodsInCheckout extends BaseQuickAdapter<GoodsDetailBean, BaseViewHolder> implements FlexibleDividerDecoration.PaintProvider {
    private int mode = AppConstant.GOODS_MODE_SHOW;

//    private Map<String, EditText> etMap;
//    private Map<String, LbGoodsNumSelector> selectorMap;

    public AdapterGoodsInCheckout(int layoutResId, List<GoodsDetailBean> data, int mode) {
        super(layoutResId, data);
        this.mode = mode;
//        etMap = new HashMap<>();
//        selectorMap = new HashMap<>();
    }

    @Override
    protected void convert(final BaseViewHolder helper, final GoodsDetailBean item) {
        View view = helper.itemView;
        SwipeRevealLayout revealLayout = view.findViewById(R.id.swipe_reveal_layout);
        TextView tvProductName = view.findViewById(R.id.tv_product_name);
        TextView tvSize = view.findViewById(R.id.tv_size);
        TextView tvPrice = view.findViewById(R.id.tv_price);
        TextView tvSum = view.findViewById(R.id.tv_sum);
        TextView tvOriginPrice = view.findViewById(R.id.tv_price_origin);
        EditText etDiscount = view.findViewById(R.id.et_money);
        ImageView ivProductPic = view.findViewById(R.id.iv_product);
        RelativeLayout layoutDelete = view.findViewById(R.id.delete_layout);
        LbGoodsNumSelector numSelector = view.findViewById(R.id.selector_goods_num);
        RelativeLayout layoutDiscount = view.findViewById(R.id.layout_discount);
        final CheckBox checkBox = view.findViewById(R.id.checkbox);
        tvOriginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        revealLayout.close(true);
        if (mode == AppConstant.GOODS_MODE_EDIT) {
            //编辑模式
            tvSum.setVisibility(View.GONE);
            numSelector.setVisibility(View.VISIBLE);
            layoutDiscount.setVisibility(View.VISIBLE);
            revealLayout.setLockDrag(true);
        } else if (mode == AppConstant.GOODS_MODE_SHOW) {
            //显示模式
            tvSum.setVisibility(View.VISIBLE);
            numSelector.setVisibility(View.GONE);
            layoutDiscount.setVisibility(View.GONE);
            revealLayout.setLockDrag(false);
        } else if (mode == AppConstant.GOODS_MODE_PURE) {//不要checkbox
            tvSum.setVisibility(View.VISIBLE);
            numSelector.setVisibility(View.GONE);
            layoutDiscount.setVisibility(View.GONE);
            checkBox.setVisibility(View.GONE);
            revealLayout.setLockDrag(true);
        }
        etDiscount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mOnItemEditTextChangedListener != null) {
                    mOnItemEditTextChangedListener.onEditTextAfterTextChanged(s, helper.getLayoutPosition());
                }
            }
        });
        SkuBean skuBean = item.getCurSkuBean();
        if (skuBean.getDiscountPrice() != skuBean.getPrice()) {
            tvOriginPrice.setVisibility(View.VISIBLE);
            tvOriginPrice.setText("￥" + skuBean.getPrice());
            tvPrice.setText("￥" + skuBean.getDiscountPrice());
            etDiscount.setText("" + skuBean.getDiscountPrice());
        } else {
            tvOriginPrice.setVisibility(View.GONE);
            tvPrice.setText("￥" + skuBean.getPrice());
            etDiscount.setText("" + skuBean.getDiscountPrice());
        }
        checkBox.setChecked(item.isChecked());
        if (skuBean != null) {
            Glide.with(mContext).asBitmap().load(item.getCurSkuBean().getColorPicUrl()).apply(ImageLoaderUtils.getSGCommonGlideOption(4)).into(ivProductPic);
        } else {
            Glide.with(mContext).asBitmap().load(item.getProductPicUrl()).apply(ImageLoaderUtils.getSGCommonGlideOption(4)).into(ivProductPic);
        }
        tvSize.setText(String.format(mContext.getResources().getString(R.string.format_string_size), skuBean.getColor(), skuBean.getSize()));
        tvProductName.setText(item.getProductName());
        tvSum.setText("x" + item.getSum());
        numSelector.setInitNumber("" + item.getSum());
        numSelector.setOnNumberChangeListener(new LbGoodsNumSelector.OnNumberChangeListener() {
            @Override
            public void onNumberChange(int number) {
                item.setSum(number);
            }
        });
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemCheckedListener != null) {
                    mOnItemCheckedListener.onItemCheck(item, checkBox.isChecked());
                }
            }
        });

        layoutDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnDeleteClickListener != null) {
                    mOnDeleteClickListener.onDeleteClick(item);
                }
            }
        });
    }

    public void setMode(int mode) {
        this.mode = mode;
        notifyDataSetChanged();
    }

    public void toggleCheck(boolean checked) {
        for (GoodsDetailBean bean : mData) {
            bean.setChecked(checked);
        }
        notifyDataSetChanged();
    }

//    public Map<String, EditText> getEtDiscountMap() {
//        return etMap;
//    }
//
//    public Map<String, LbGoodsNumSelector> getNumSelectorMap() {
//        return selectorMap;
//    }

    public interface OnItemCheckedListener {
        void onItemCheck(GoodsDetailBean item, boolean checked);
    }

    private OnItemCheckedListener mOnItemCheckedListener;

    public void setOnItemCheckedListener(OnItemCheckedListener listener) {
        this.mOnItemCheckedListener = listener;
    }

    public interface OnDeleteClickListener {
        void onDeleteClick(GoodsDetailBean item);
    }

    private OnDeleteClickListener mOnDeleteClickListener;

    public void setOnDeleteClickListener(OnDeleteClickListener listener) {
        mOnDeleteClickListener = listener;
    }

    private OnItemEditTextChangedListener mOnItemEditTextChangedListener;

    public interface OnItemEditTextChangedListener {
        void onEditTextAfterTextChanged(Editable editable, int position);
    }

    public void setOnItemEditTextChangedListener(OnItemEditTextChangedListener listener) {
        mOnItemEditTextChangedListener = listener;
    }

    @Override
    public Paint dividerPaint(int position, RecyclerView parent) {
        Paint paint = new Paint();
        paint.setColor(mContext.getResources().getColor(R.color.sg_common_line_bg));
        paint.setStrokeWidth(10);
        return paint;
    }
}
